<template>
  <el-dialog
    title="选择试衣分类"
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
    append-to-body
  >
    <div class="select-category-container">
      <div class="tree-container">
        <el-tree
          ref="tree"
          :data="treeData"
          :props="{
            children: 'childList',
            label: 'name'
          }"
          node-key="aiClothesType"
          highlight-current
          @node-click="handleNodeClick"
        ></el-tree>
      </div>
      <div class="preview-container">
        <div class="preview-title">预览图</div>
        <div class="preview-image">
          <img v-if="previewImage" :src="previewImage" alt="预览图"/>
          <div v-else class="no-image">
            <i class="el-icon-picture-outline"></i>
            <p>暂无预览图</p>
          </div>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { productAiFittingCategoryLevelMaskApi } from '@/api/product';
export default {
  name: 'SelectCategory',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: [String, Number],
      default: ''
    },
    treeData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogVisible: false,
      selectedNode: null,
      previewImage: ''
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val;
      if (val && this.value) {
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.value);
          // 找到当前选中的节点
          this.findAndSetSelectedNode(this.treeData, this.value);
        });
      }
    },
    dialogVisible(val) {
      if (!val) {
        this.$emit('update:visible', false);
      }
    }
  },
  methods: {
    async getMaskImage() {
      const res = await productAiFittingCategoryLevelMaskApi(this.selectedNode.id);
      // console.log('res', res);
      this.previewImage = res;
    },
    handleClose() {
      this.dialogVisible = false;
      this.$emit('update:visible', false);
    },
    handleNodeClick(data) {
      // 只有当节点没有子节点时才能选择
      if (!data.childList || data.childList.length === 0) {
        this.selectedNode = data;
        console.log(data,95);
        this.getMaskImage();
      } else {
        this.selectedNode = null;
        // this.$message.warning('请选择最后一级分类');
      }
    },
    handleConfirm() {
      if (!this.selectedNode) {
        this.$message.warning('请选择最后一级分类');
        return;
      }
      if (this.selectedNode.childList && this.selectedNode.childList.length > 0) {
        this.$message.warning('请选择最后一级分类');
        return;
      }
      this.$emit('input', this.selectedNode.aiClothesType);
      this.$emit('select', this.selectedNode);
      this.handleClose();
    },
    findAndSetSelectedNode(nodes, value) {
      for (let node of nodes) {
        if (node.aiClothesType === value && (!node.childList || node.childList.length === 0)) {
          this.selectedNode = node;
          return true;
        }
        if (node.childList && node.childList.length) {
          if (this.findAndSetSelectedNode(node.childList, value)) {
            return true;
          }
        }
      }
      return false;
    }
  }
}
</script>

<style lang="scss" scoped>
.select-category-container {
  display: flex;
  min-height: 400px;
  
  .tree-container {
    flex: 1;
    border-right: 1px solid #EBEEF5;
    padding-right: 20px;
    overflow: auto;
  }
  
  .preview-container {
    flex: 1;
    padding-left: 20px;
    
    .preview-title {
      font-size: 14px;
      color: #606266;
      margin-bottom: 15px;
    }
    
    .preview-image {
      width: 100%;
      height: 300px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #f5f7fa;
      border-radius: 4px;
      
      img {
        max-width: 100%;
        max-height: 100%;
        object-fit: contain;
      }
      
      .no-image {
        text-align: center;
        color: #909399;
        
        i {
          font-size: 48px;
          margin-bottom: 10px;
        }
        
        p {
          margin: 0;
          font-size: 14px;
        }
      }
    }
  }
}
</style> 